package logic

import (
	"context"

	"paper-pro/app/paperfile/model"
	"paper-pro/app/paperfile/rpc/internal/svc"
	"paper-pro/app/paperfile/rpc/pb/paperfile"
	"paper-pro/pkg/xerr"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateParseStatusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateParseStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateParseStatusLogic {
	return &UpdateParseStatusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateParseStatusLogic) UpdateParseStatus(in *paperfile.UpdateParseStatusReq) (*paperfile.UpdateParseStatusResp, error) {
	parseStatus := in.VectorDBInfo.ParseStatus
	if !model.IsValidParseStatus(parseStatus) {
		return nil, xerr.NewErrCodeMsg(xerr.REQUEST_PARAM_ERROR, "invalid parse status: "+parseStatus)
	}

	if _, err := l.svcCtx.PaperModel.UpdateParseStatus(l.ctx, in.Id, parseStatus, in.UserId); err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "UpdateParseStatus DB err: %+v", err)
	}

	return &paperfile.UpdateParseStatusResp{}, nil
}
